package com.game.utils;

import com.game.config.Config;

import android.app.Activity;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;

public class GamePreference {
	public static GamePreference instance;
	public  SharedPreferences sp;
	
	public GamePreference(Activity context){
		Log.i("infos", "初始化GamePreference");
		sp=PreferenceManager.getDefaultSharedPreferences(context);
		instance=this;
		//读取所有设置
		Config.ReadAllConfigs();
	}
	
	//取设置----------------------------------------------------------------
	public String getConfigString(int configIndex){
		return sp.getString(Config.GAME_PREFERENCE[configIndex], null);
	}
	
	public boolean getConfigBoolean(int configIndex){
		return sp.getBoolean(Config.GAME_PREFERENCE[configIndex],false);
	}
	
	public int getConfigInteger(int configIndex){
		return sp.getInt(Config.GAME_PREFERENCE[configIndex], 0);
	}
	
	public float getConfigFloat(int configIndex){
		return sp.getFloat(Config.GAME_PREFERENCE[configIndex], 0);
	}
	
	//保存设置----------------------------------------------------------------
	public void saveConfigString(int configIndex,String value){
		SharedPreferences.Editor editor=sp.edit();
		editor.putString(Config.GAME_PREFERENCE[configIndex], value);
		editor.commit();
	}
	
	public void saveConfigBoolean(int configIndex,boolean value){
		SharedPreferences.Editor editor=sp.edit();
		editor.putBoolean(Config.GAME_PREFERENCE[configIndex], value);
		editor.commit();
	}
	
	public void saveConfigInteger(int configIndex,int value){
		SharedPreferences.Editor editor=sp.edit();
		editor.putInt(Config.GAME_PREFERENCE[configIndex], value);
		editor.commit();
	}
	
	public void saveConfigFloat(int configIndex,float value){
		SharedPreferences.Editor editor=sp.edit();
		editor.putFloat(Config.GAME_PREFERENCE[configIndex], value);
		editor.commit();
	}
	
	/**
	 * 保存所有*/
	public void saveAll(Object[] values){
		SharedPreferences.Editor editor=sp.edit();
		for(int i=0;i<Config.GAME_PREFERENCE.length;i++){
			Object value = values[i];
			if(value!=null){
				if( value instanceof String){
					editor.putString(Config.GAME_PREFERENCE[i], (String)value);
				}else if(value instanceof Integer){
					editor.putInt(Config.GAME_PREFERENCE[i], (Integer)value);
				}else if(value instanceof Boolean){
					editor.putBoolean(Config.GAME_PREFERENCE[i], (Boolean)value);
				}else if(value instanceof Float){
					editor.putFloat(Config.GAME_PREFERENCE[i], (Float)value);
				}
			}
		}
		editor.commit();
	}
}